package yl.hs.bmipfull.controllers.equipment;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.equipment.EquipmentEntity;
import yl.hs.bmipfull.service.equipment.IEquipmentService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/*
 * EquipmentController
 * @category
 * @author 谢景广
 * @date 2022-11-14
 */
@Api(value = "设备信息操作接口", tags = "设备管理——设备信息接口")
@RestController
@RequestMapping("/api/equipment/info")
public class EquipmentController extends ControllerBase {
    @ApiOperation(value = "根据查询条件获取设备信息的分页信息", notes = "获取设备的分页信息")
    @PostMapping("{rowNum}/{page}")
    public WebResult query(@PathVariable int rowNum, @PathVariable int page, @RequestBody PageQuery query) {

        try {
            PageResult<EquipmentEntity> result = equipmentService.queryPage(query, rowNum, page);
            svSvc.updateUserMenuView(query);
            return success("ok", result);
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "保存设备信息", notes = "保存新建或者更改后的设备信息")
    @PostMapping("/save")
    public WebResult save(@RequestBody SubmitModel model){
        try{
            Integer result = equipmentService.saveEquipment(model);
            if(result>0){
                return success("保存成功");
            }
            else
            {
                return fail("保存失败");
            }
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "根据分组信息查询设备信息", notes = "根据分组信息查询设备信息")
    @GetMapping("/querybyordersn/{sn}")
    public WebResult queryByorderSn(@PathVariable String  sn){
        try{
            var  result = equipmentService.queryByorderSn(sn);
            return success("查询成功",result);
        }
        catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "删除设备信息", notes = "根据编号删除设备信息")
    @DeleteMapping("/delete/{sn}")
    public WebResult delete(@PathVariable("sn")String sn){
        try{
            var snArray = sn.split(",");
            equipmentService.delete(snArray);
            return success("删除成功");
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "获取设备维护信息", notes = "根据编号获取设备维护信息")
    @GetMapping("/allRecord/{sn}")
    public WebResult queryAllRecord(@PathVariable String sn){
        try{
            return success("ok",equipmentService.queryEquipmentMRRecord(sn));
        }
        catch (Exception ee){
            return error(ee);
        }
    }

    @ApiOperation(value = "获取设备日志信息", notes = "获取设备日志信息")
    @GetMapping("/log/{sn}")
    public WebResult getLog(@ApiParam(value = "数据编号") @PathVariable String sn){
        return success("查询成功", equipmentService.queryLogByDataSn(sn, ""));
    }
    @ApiOperation(value = "手动检测设备维护周期", notes = "手动检测设备维护周期")
    public WebResult checkEquipment(){
        equipmentService.checkEquipment();
        return success("检测成功");
    }

    private IEquipmentService equipmentService;

    @Autowired
    public void setEquipmentService(IEquipmentService equipmentService) {
        this.equipmentService = equipmentService;
    }

    private ISearchViewService svSvc;

    @Autowired
    public void setSvSvc(ISearchViewService svSvc) {
        this.svSvc = svSvc;
    }
}
